package com.yuju.controller.pub;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yuju.account.model.AccountReportModel;
import com.yuju.account.service.IAccountReportService;
import com.yuju.controller.base.BaseController;
import com.yuju.vo.account.AccountReportVO;

/**
 * 类名：RptGuestBalanceController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2018年2月7日 上午10:26:50  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("rptAccount")
public class RptGuestBalanceController extends BaseController<AccountReportVO, AccountReportModel> {
	
	@Autowired
	private IAccountReportService accountReportService;
	
	/**
	 * 功能：导出宾客余额报表<br/>
	 *
	 * @author jiaokn
	 * @Date 2018年2月7日 上午10:22:38 <br/>
	 */
	@RequestMapping(value = "/expGuestBalanceExcel")
	public void expGuestBalanceExcel(AccountReportVO accountReportVO) throws Exception {
		HttpServletResponse response = getResponse();
		String excelFileName = "";
		InputStream excelStream = null;
		byte[] bytes = accountReportService.expBalanceRpt(convert(accountReportVO));
		ByteArrayInputStream is = new ByteArrayInputStream(bytes);
		excelStream = is; // 文件流
		excelFileName = "宾客余额报表.xlsx"; // 设置下载的文件名
		response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((excelFileName).getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(excelStream);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
	}
	
}
